﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Skipette
{
    public partial class Startup : Form
    {
        public BaseMain main;
        Thread serverChecker;

            ToolStripMenuItem online = new ToolStripMenuItem("Online");
            ToolStripMenuItem busy = new ToolStripMenuItem("Busy");
            ToolStripMenuItem hidden = new ToolStripMenuItem("Hidden");
            ToolStripMenuItem logout = new ToolStripMenuItem("Logout");
            ToolStripSeparator separator = new ToolStripSeparator();
            ToolStripMenuItem exit = new ToolStripMenuItem("Exit");



        public Startup()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            online.Click += new EventHandler(online_Click);
            online.Image = Properties.Resources.online;
            busy.Click += new EventHandler(busy_Click);
            busy.Image = Properties.Resources.busy;
            hidden.Click+=new EventHandler(hidden_Click);
            hidden.Image = Properties.Resources.hidden;
            logout.Click += new EventHandler(logout_Click);
            logout.Image = Properties.Resources.offline;
            exit.Click += new EventHandler(exit_Click);

            SetNotifyContextMenu();
            notifyIcon1.Icon = Properties.Resources.offlineIco;
            //notifyIcon1.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            //notifyIcon1.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { online, busy, hidden, logout, separator, exit });
            notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);

            this.FormClosing += new FormClosingEventHandler(Startup_FormClosing);
            this.VisibleChanged += new EventHandler(Startup_Shown);
        }

        public void SetNotifyContextMenu()
        {
            if (Program.currentProfile != null)
            {
                notifyIcon1.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
                notifyIcon1.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { online, busy, hidden, logout, separator, exit });
            }
            else
            {
                notifyIcon1.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
                notifyIcon1.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { exit });
            }
        }

        //FOR BLINKING CONTROLS
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }

        void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.main != null)
            {
                if (!main.IsVisible())
                    main.Show();
                else
                    main.Hide();
            }
            else
            {
                if (!this.Visible)
                    this.Show();
                else
                    this.Hide();
            }
        }

        void Startup_Shown(object sender, EventArgs e)
        {
            this.Text = "Server status pending...";
            if (this.Visible)
            {
                serverChecker = new Thread(delegate()
                {
                    if (Program.TestServer())
                        this.Text = "Server: ON-line";
                    else
                        this.Text = "Server: OFF-line";
                });
                serverChecker.IsBackground = true;
                serverChecker.Start();
            }
        }

        public NotifyIcon GetNotificationIcon()
        {
            return notifyIcon1;
        }

        void Startup_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.exiting = true;
            Server.tcpListener.Stop();
            notifyIcon1.Visible = false;
        }

        void exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void logout_Click(object sender, EventArgs e)
        {
            Logout();
        }

        void hidden_Click(object sender, EventArgs e)
        {
            SetIcoStatus("Hidden");
        }

        void busy_Click(object sender, EventArgs e)
        {
            SetIcoStatus("Busy");
        }

        void online_Click(object sender, EventArgs e)
        {
            SetIcoStatus("Online");
        }

        public void SetIcoStatus(string status)
        {
            if (Program.currentProfile != null)
            {
                if (status.Equals("Online"))
                    notifyIcon1.Icon = Properties.Resources.onlineIco;
                else if (status.Equals("Busy"))
                    notifyIcon1.Icon = Properties.Resources.busyIco;
                else if (status.Equals("Hidden"))
                    notifyIcon1.Icon = Properties.Resources.hiddenIco;
                else if (status.Equals("Offline"))
                    notifyIcon1.Icon = Properties.Resources.offlineIco;
                Program.currentStatus = Program.StatusFromString(status);
                main.UpdateStatuses();
            }
        }

        public void SetIcoStatus(Skipette.Program.STATUS status)
        {
            if (Program.currentProfile != null)
            {
                if (status== Program.STATUS.ONLINE)
                    notifyIcon1.Icon = Properties.Resources.onlineIco;
                else if (status== Program.STATUS.BUSY)
                    notifyIcon1.Icon = Properties.Resources.busyIco;
                else if (status== Program.STATUS.HIDDEN)
                    notifyIcon1.Icon = Properties.Resources.hiddenIco;
                else if (status== Program.STATUS.OFFLINE)
                    notifyIcon1.Icon = Properties.Resources.offlineIco;
                Program.currentStatus = status;
                main.UpdateStatuses();
            }
        }

        public void Logout()
        {
            Program.currentProfile = null;
            notifyIcon1.Icon = Properties.Resources.offlineIco;
            SetNotifyContextMenu();
            main.SetSafeClose(true);
            main.Close();
            main = null;
            this.Show();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            Skipette.Program.STATUS desired = Program.STATUS.OFFLINE;
            if (comboBoxLogin.SelectedIndex == 0)
                desired = Program.STATUS.ONLINE;
            else if (comboBoxLogin.SelectedIndex == 1)
                desired = Program.STATUS.BUSY;
            else if (comboBoxLogin.SelectedIndex == 2)
                desired = Program.STATUS.HIDDEN;
            if (Program.LogIn(textBoxUname.Text, textBoxPass.Text, desired) == null)
            {
                labelFailed.Show();
            }
            else
            {
                SetNotifyContextMenu();
                serverChecker.Abort();
                labelFailed.Hide();
                this.Hide();
                if (!Program.compact)
                    main = new FormMain(this);
                else
                    main = new FormCompact(this);
                SetIcoStatus(comboBoxLogin.SelectedItem.ToString());
                main.Show();
            }
        }

        private void comboBoxLogin_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxLogin.SelectedItem.Equals("Online"))
                panelLogin.BackgroundImage = Properties.Resources.online;
            else if (comboBoxLogin.SelectedItem.Equals("Busy"))
                panelLogin.BackgroundImage = Properties.Resources.busy;
            else if (comboBoxLogin.SelectedItem.Equals("Hidden"))
                panelLogin.BackgroundImage = Properties.Resources.hidden;
        }

        private void checkBoxRemember_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void labelNew_Click(object sender, EventArgs e)
        {

        }
    }
}
